package com.zy.openchat.util;

import android.Manifest;
import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.zy.openchat.App;

import java.util.List;

public class LocationHelper {

    private String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

    private volatile static LocationHelper locationHelper;
    public static LocationHelper getIntance() {
        if (locationHelper == null) {
            synchronized (LocationHelper.class) {
                if (locationHelper == null) {
                    locationHelper = new LocationHelper();
                }
            }
        }
        return locationHelper;
    }
    private AMapLocation currentLocation;
    private AMapLocationClient mLocationClient;

    public void getLocation(Context context, AMapLocationListener listener){
        if(!XXPermissions.isGranted(context, permissions)){
            XXPermissions.with(context).permission(permissions).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    getInnerLocation(listener);
                }

                @Override
                public void onDenied(List<String> permissions, boolean never) {
                    listener.onLocationChanged(null);
                }
            });
        }else{
            getInnerLocation(listener);
        }

    }

    public void getInnerLocation(AMapLocationListener listener){
        if(currentLocation!=null){
            listener.onLocationChanged(currentLocation);
        }else{
            startOnceLocation(listener);
        }
    }
    public void startLocation(Context context){
        if(!XXPermissions.isGranted(context, permissions)){
            XXPermissions.with(context).permission(permissions).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    realStartLocation();
                }
            });
        }else{
            realStartLocation();
        }
    }


    private void realStartLocation(){
        if(mLocationClient!=null){
            stopLocation();
        }
        try {
            mLocationClient = new AMapLocationClient(App.getInstanse());
            //设置定位回调监听，这里要实现AMapLocationListener接口，AMapLocationListener接口只有onLocationChanged方法可以实现，用于接收异步返回的定位结果，参数是AMapLocation类型。
            mLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    currentLocation = aMapLocation;
//                TelegramManager.getTelegramManager().setLocation(aMapLocation);
                }
            });
            //初始化定位参数
            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
            //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(false);
            //设置是否强制刷新WIFI，默认为强制刷新
            mLocationOption.setWifiActiveScan(true);
            //设置是否允许模拟位置,默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(2000);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
        }catch (Exception e){
            e.printStackTrace();
        }
//        currentLocation = new AMapLocation("");
//        currentLocation.setLongitude(30.287268);
//        currentLocation.setLongitude(119.965739);
//        currentLocation.setAccuracy(0f);
//        TelegramManager.getTelegramManager().setLocation(currentLocation);

    }


    private void startOnceLocation(AMapLocationListener listener){
//        currentLocation = new AMapLocation("");
//        currentLocation.setLongitude(30.287268);
//        currentLocation.setLongitude(119.965739);
//        currentLocation.setAccuracy(0f);
//        listener.onLocationChanged(currentLocation);
        try {
            AMapLocationClient mLocationClient = new AMapLocationClient(App.getInstanse());
            //设置定位回调监听，这里要实现AMapLocationListener接口，AMapLocationListener接口只有onLocationChanged方法可以实现，用于接收异步返回的定位结果，参数是AMapLocation类型。
            mLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    if(listener!=null){
                        listener.onLocationChanged(aMapLocation);
                    }

                    currentLocation = aMapLocation;
//                TelegramManager.getTelegramManager().setLocation(aMapLocation);
                }
            });
            //初始化定位参数
            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
            //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(true);
            //设置是否强制刷新WIFI，默认为强制刷新
            mLocationOption.setWifiActiveScan(true);
            //设置是否允许模拟位置,默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void stopLocation(){
        currentLocation = null;
        try {
            if(mLocationClient!=null){
                mLocationClient.stopLocation();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        mLocationClient = null;
    }

    public static String distanceFormat(long distance) {
        String str;
        long value = distance;
        if (distance >= 1000) {
            value = value / 1000;
            str = "km";
        } else {
            str = "m";
        }
        return String.format("%s%s",String.valueOf(value),str);
    }

}
